﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TeacherHelp.FormList
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
        public Form OpenForm = null;
        private void LoginBtn_Click(object sender, EventArgs e)
        {
            Model.User user = new Model.User();
            user.LoginName = LoginNameTxt.Text.Trim();
            user.LoginPwd = LoginPwdTxt.Text.Trim();
            if (string.IsNullOrEmpty(user.LoginName) || string.IsNullOrEmpty(user.LoginPwd))
            {
                MessageBox.Show("用户名和密码不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var Info = new BLL.LoginManager().Login(user);
            if (string.IsNullOrEmpty(Info.Item1))
            {
                switch (Info.Item2)
                {
                    //管理员
                    case 0:
                    case 1:
                        OpenForm = new Admin.AdminMainForm();
                        break;
                    case 2:
                        OpenForm = new Teacher.TeacherMainForm();
                        break;
                    case 3:
                        OpenForm = new Student.StudentMainForm();
                        break;
                }
                Program.UserId = Info.Item3;


                Close();
            }
            else
            {
                MessageBox.Show(Info.Item1, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (OpenForm == null) Application.ExitThread();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void MinBtn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void panelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //窗体拖动
                ReleaseCapture();
                SendMessage(Handle, 0xA1, 0x02, 0);
            }
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;


        private void LoginNameTxt_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Task.Run(() => RunDosCommand("osk"));

        }
        public static string RunDosCommand(string command)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine(command);
            process.StandardInput.WriteLine("exit");
            return process.StandardOutput.ReadToEnd();
        }
    }
}
